#include "testDrive.h"

PNode createNode(string line, Node *ptr, int lineNumber){
	PNode node = (PNode)malloc(sizeof(Node));
	node->line = (char*)malloc(line.size()+1);
	node->line[line.size()]=0;
	memcpy(node->line, line.c_str(), line.size());
	node->lineNumber = lineNumber;
	node->next = ptr;
	return node;
}

void freeNode( PNode node){
	free(node->line);
	free(node);
}

void freeList( PNode node){
	if ( node->next != NULL )
		freeList( node->next);

	freeNode( node);
}

PNode readFile( string fileName){
	string line;
	ifstream infile;
	Node *head = NULL, *tmpNode = NULL; 
	int lineNumber = 1;
	infile.open (fileName);
	while (getline(infile, line))
	{
		head = createNode(line, head, lineNumber++);
	}
	infile.close();
	return head;
}

void splitByComma( string *arr, string line){
	int index = 0;
	istringstream ss(line);
	string token;
	while(getline(ss, token, ',')) {
			arr[index++] = token;
	}
}